package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.AddressCtr;//The TUILayer needs to know the ControlLayer
import ModelLayer.Person;

/**
 * User interface class to the AddressBook
 * 
 * @author Gruppe A
 * @version 1.0
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer

    /**
     * Constructor for the AddressBookUI
     */
    public AddressBookUI()
    {
        addrCtr = new AddressCtr();
    }

    /**
     * A method that starts the address menu
     */
    public void start()
    {

        addressMenu();
    }

    /**
     * A method that creates our address menu, it does this by calling our private commands later down both for input and to execute methods. It does this by creating
     * a menu where you have the possibility of inputting different numbers, it has some reduncy inbuilt in regards to the user not being able to use invalid numbers.
     */
    public void addressMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeAddressBookMenu(); 

            if(choice != 1 && choice != 2 && choice !=3 && choice !=4 && choice != 5 && choice !=6)
            {
                System.out.println("You have to choose either 1, 2, 3, 4, 5 or 6");
            }
            if(choice == 1)
            {   //show person
                getPerson();
            }
            else{
                if(choice == 2){
                    createPerson();
                }  
                else{
                    if(choice == 3){
                        deletePerson();
                    }  
                    else{
                        if(choice == 4){
                            updatePerson();
                        }
                        else{
                            if(choice == 5){
                                listAllPersons();
                            }
                            else{
                                if(choice == 6)
                                {
                                    exit = true;
                                }
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else

        }//end while
    }

    /**
     * This method is our primary user interface for the addressbook, it's prints a screen with the choices the user can make, it then takes that input and returns it
     * so that the command can be executed.
     * It also has a while loop implemented that checks wether or not the input is valid number or if it's an invalid command, in which case they will be directed to using
     * the correct input method.
     * @return int choice
     */
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" *** Address Book Menu ***");
        System.out.println(" (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        int choice = 0;
        boolean ok = false;
        while(!ok)
        {
            try{
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }

        }//end while
        return choice;
    }

    //the method reads id for a person and return
    /**
     * A method for reading the input regarding a persons id, it also returns the persons id for use in other methods.
     * It has a while loop implemented that checks wether or not the input is valid number or if it's an invalid command, in which case they will be directed to using
     * the correct input method.
     * @return long id
     */
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Id: ");
            try{
                id = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }

    /**
     * A method for inputting a persons name. It also returns the name for use in other methods
     * @return String name
     */
    public String inputName()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write Name: ");
        String name = keyboard.nextLine();
        return name;
    }

    /**
     * A method for inputting a persons postal code, it returns the persons postal code to be used in other commands
     * It also has a while loop implemented that checks wether or not the input is valid number or if it's an invalid command, in which case they will be directed to using
     * the correct input method.
     * @return int code
     */
    public int inputPostalCode()
    {
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);  
            System.out.println(" Write Postal Code: ");
            try{
                code = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return code;
    }

    /**
     * A method for inputting a persons city, it returns the persons city to be used in other commands
     * @return String city
     */
    public String inputCity()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write City: ");
        String city = keyboard.nextLine();
        return city;
    } 

    /**
     * A method for inputting a persons phone number, it also returns it for use in other commands
     * @return String phone
     */
    public String inputPhone()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write Phone Number: ");
        String phone = keyboard.nextLine();
        return phone;
    } 

    /**
     * A method for inputting a persons address, it also returns it for use in other commands
     * @return String address
     */
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }

    /**
     * A method for showing a persons id
     * @param long id
     */
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }

    /**
     * A method for showing a persons name
     * @param String name
     */
    public void showName(String name)
    { 
        System.out.println("The name of the person: " + name);
    }

    /**
     * A method for showing a persons address
     * @param String address
     */
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }

    /**
     * A method for finding a person, it uses the input from several of our commands in order to locate the person, and then prints information relating to that person,
     * if no person is found it then returns an error message
     */
    private void getPerson()
    {//is to be changed when the address book controller is completed
        long id = inputPersonsID();
        Person p = addrCtr.getPerson(id);

        if(p != null)
        {
            System.out.println("***The following information regarding:");
            System.out.println(p.getName());
            System.out.println("Address: "+p.getAddress());
            System.out.println("Postal Number: "+p.getPostnr());
            System.out.println("City: "+p.getCity());
            System.out.println("Phone Number: "+p.getPhoneNumber());
            System.out.println("ID :"+p.getId());
            System.out.println("\n");
        }
        else
        {
            System.out.println("\n");
            System.out.println("No person with that ID exists");
            System.out.println("\n");
        }//end else

    }  

    /**
     * A method for creating a person it uses the input from numerous of our previous methods, in order to fill in all the relevant data. After the person has been created
     * and added to our AddressBook it then prints a message to let the user know the person has been created.
     */
    private void createPerson()
    {
        String name = inputName();
        String address = inputAddress();
        long id = inputPersonsID();
        int postnr = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();

        addrCtr.createPerson(id, name, address, postnr, city, phone);
        System.out.println("\n");
        System.out.println("The Person has been created");
        System.out.println("\n");
    }

    /**
     * A method for deleting a person, it uses the input from our person id to find the person, and then deletes that specific person, after completion it prints a message
     * to the user that the person has been deleted. If no person was found it instead prints an error message.
     */
    private void deletePerson()
    {
        long id = inputPersonsID();
        Person p = addrCtr.getPerson(id);
        if(p != null)
        {
            addrCtr.deletePerson(id);
            System.out.println("\n");
            System.out.println("The person has been deleted from the system");
            System.out.println("\n");
        }
        else
        {
            System.out.println("\n");
            System.out.println("The specified person couldn't be found");
            System.out.println("\n");
        }//end else
    }

    /**
     * A method for updating a specific person, it uses almost all of our input commands in order to both find and update the person, after the person has been found 
     * and updated it then prints all the new information to the user. If no person is found it prints an error message.
     */
    private void updatePerson()
    {
        long id = inputPersonsID();
        String name = inputName();
        String address = inputAddress();
        int postnr = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();
        Person p = addrCtr.getPerson(id);

        if(p != null)
        {
            addrCtr.updatePerson(id,name,address,postnr,city,phone);
            System.out.println("New name: "+p.getName());
            System.out.println("New address: "+p.getAddress());
            System.out.println("New postal number: "+p.getPostnr());
            System.out.println("New city: "+p.getCity());
            System.out.println("New phone number: "+p.getPhoneNumber());
        }
        else
        {
            System.out.println("\n");
            System.out.println("The specified person couldn't be found");
            System.out.println("\n");
        }//end else

    }
    
    /**
     * A method for printing all the people in our addressbook
     */
    private void listAllPersons()
    {
        System.out.println(addrCtr.listAllPersons()+"\n"); 
    }
}
